<template>
  <div class="app-container">
    <div class="common-card damConfigForm">
      <div class="title mb-5">查看公告</div>
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="180px" :disabled = true>
        <el-row>
          <el-col :span="8">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>  
          <el-col :span="8">
            <el-form-item label="内容" prop="detail">
              <el-input v-model="form.detail" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>  
          <el-col :span="8">
            <el-form-item label="发布时间" prop="createTime">
              <el-input v-model="form.createTime"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="16">
            <el-form-item label="图片">
              <div class="uploadArea">
                <input
                  ref="fileRef"
                  type="file"
                  name="upload"
                  class="uploadBtn"
                  accept=".png, .jpeg, .jpg"
                  @change="fileChange"
                >
                <div v-if="!picUrl" class="iconBox">
                  <i class="el-icon-upload icon" />
                </div>
                <img v-else :src="picUrl" alt="" class="previewImg">
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="actionBox">
        <el-button type="success" @click="handleSubmit">保存</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getAnnouncementDetailById, addAnnouncement, updateAnnouncement } from '@/api/announcement'

import { uploadFile } from '@/api/common'

export default {
  data() {
    return {
      picUrl: '',
      picFile: null,
      form: { },
      rules: {
        title: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        supervisorPhone: [
          { required: true, message: '请输入负责手机号码', trigger: 'blur' },
          {
            pattern: /$/,
            message: '请检查手机号码格式',
            trigger: 'blur'
          }
        ],
        guaranteePeriod: [
          
        ],
        supervisorTel: [
          {
            pattern: /^\d{3}-\d{7,8}|\d{4}-\d{7,8}$/,
            message: '请检查固定电话格式',
            trigger: 'blur'
          }
        ],
        normalTime: [
          { required: true, message: '请输入平均无故障时间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isEdit() {
      if (this.$route.params && this.$route.params.id) {
        return true
      }
      return false
    },
    id() {
      return this.$route.params && this.$route.params.id
    }
  },
  created() {
    if (this.isEdit) {
      getAnnouncementDetailById(this.id).then(res => {
        this.form = res
        this.picUrl = res.pic
      })      
    }
  },
  mounted() {
    
  },
  methods: {
    handleSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            updateAnnouncement(this.form).then((res) => {
              if (res) {
                this.$message.success('编辑商品成功')
                this.handleCancel()
              } else {
                this.$message.error('编辑商品失败')
              }
            })
          } else {
            addAnnouncement(this.form).then((res) => {
              if (res) {
                this.$message.success('新增商品成功')
                this.handleCancel()
              } else {
                this.$message.error('新增商品失败')
              }
            })
          }
        }
      })
    },
    handleInput(value) {
      this.form.guaranteePeriod = value.replace(/^(0+)|[^\d]+/g, '')
    },
    handleCancel() {
      this.$router.push({
        name: 'AnnouncementList',
      })
    },
    handleUpload() {
      this.$refs.fileRef.click()
    },
    fileChange(e) {
      this.picFile = e.target.files[0]
      // 限定上传图片类型及大小
      if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(e.target.value)) {
        this.$message.error('请上传.png/.jpg/.jpeg格式的图片')
        return
      }
      if (this.picFile.size / 1024 > 1024 * 10) {
        this.$message.error('请上传不超过10m的图片')
        return
      }

      const formData = new FormData()
      formData.append('uploadFile', this.picFile)
      uploadFile(formData).then((res) => {
        if (res.status === 200) {
          const url = res.data.annexPath
          this.form.pic = url
          this.picUrl = url
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.damConfigForm {
  padding: 40px;
  .title {
    font-size: 20px;
    font-weight: 800;
  }
  .actionBox {
    display: flex;
    justify-content: center;
  }
}
.uploadArea {
  height: 480px;
  background-color: #ccc;
  cursor: pointer;
  border-radius: 4px;
  .iconBox {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .previewImg {
    height: 100%;
    width: 100%;
  }
  .uploadBtn {
    display: none;
  }
  .icon {
    font-size: 80px;
    color: #888;
  }
}
</style>